$(function() {
	$('.sidebar.col-md-2 a').on('click', function(e) {
		var tar = $(e.target),
			tg = tar.attr('target');
		//隐藏其他，并显示当前
		$('.main.col-md-10[code]').hide();
		$('.main.col-md-10[code=' + tg + ']').show();
		tg ? window['initCode' + tg]() : '';
	});
	$('.sidebar.col-md-2 a[target="1"]').click();
});
/**
 * 基础数据（区分不同渠道）
 */
function initCode1() {
		//各类渠道下载
		getDataInterFace({
			code: 1,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var data = [],
				$Container = $('.downloads-channel-container'),
				$char,
				leg = [],
				jd = jStaticData.data;
			for (var i = 0, l = jd.length; i < l; i++) {
				$char = $('.downloads-channel-day[target=' + jd[i]['channel'] + ']', $Container);
				$('.panel-heading i', $char).html(jd[i]['channel']);
				$('.panel-body-text em', $char).html(jd[i]['downloads']);
				$('.panel-body-text sub', $char).html(parseFloat(jd[i]['downloads'] * 100 / jStaticData['total']).toFixed(2) + '%');
			}
		});
		//日激活量
		getDataInterFace({
			code: 1,
			type: 2
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data;
			var data = [{
				value: jd['activation'],
				name: '激活数'
			}, {
				value: jd['downloads'],
				name: '下载数'
			}];
			initEcharts({
				render: 'active-day-chart',
				type: 'pie',
				text: parseFloat(jd['activation'] * 100 / jd['downloads']).toFixed(2) + '%',
				series: [{
					radius: ['72%', '90%'],
					data: data
				}]
			});
		});
		//日新增
		getDataInterFace({
			code: 1,
			type: 3
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data;
			$('.new-day .number-font').html(jd['newusers']);
		});
		//当天新注册用户后存留的
		getDataInterFace({
			code: 1,
			type: 4
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [];
			data.push(jd['newusers']);
			data.push(jd['retained2']);
			data.push(jd['retained4']);
			data.push(jd['retained8']);
			data.push(jd['retained31']);

			initEcharts({
				render: 'retained-day-chart',
				type: 'line',
				xAxis: [{
					data: ['当日新注册', '第二天', '第四天', '第八天', '第三十一天']
				}],
				yAxis: [{
					axisLabel: {
						formatter: function(value) {
							return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
						}
					}
				}],
				series: [{
					data: data
				}]
			});
		});
		//日新增
		getDataInterFace({
			code: 1,
			type: 8
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data;
			var precent = parseFloat(jd['loginusers'] * 100 / jd['newusers']).toFixed(2);
			$('.login-day .progress-bar').css({
				'width': precent
			});
			$('.login-day .progress-bar .sr-only').html(precent + '%');
		}); //日活跃
		getDataInterFace({
			code: 1,
			type: 9
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data;
			$('#vivacious-day-chart').html('<h6>' + jd['loginusers'] + '</h6><em class="number-font">/' + jd['newusers'] + '</em>');
		});
		//月登录
		getDataInterFace({
			code: 1,
			type: 10
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data;
			var data = [{
				value: jd['loginusers'],
				name: '登录'
			}, {
				value: Math.abs(jd['newusers'] - jd['loginusers']),
				name: ''
			}];
			initEcharts({
				render: 'login-month-chart',
				type: 'pie',
				text: parseFloat(jd['loginusers'] * 100 / jd['newusers']).toFixed(2) + '%',
				series: [{
					radius: ['0%', '100%'],
					data: data
				}]
			});
		});
		//月活跃
		getDataInterFace({
			code: 1,
			type: 11
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [{
					value: jd['vivacious'],
					name: '活跃'
				}, {
					value: Math.abs(jd['newusers'] - jd['vivacious']),
					name: ''
				}];
			initEcharts({
				render: 'vivacious-month-chart',
				type: 'pie',
				text: parseFloat(jd['vivacious'] * 100 / jd['newusers']).toFixed(2) + '%',
				series: [{
					radius: ['0%', '100%'],
					data: data
				}]
			});
		});
	}
	/**
	 * 用户使用环境
	 */

function initCode2() {
		//活跃用户登陆设备
		getDataInterFace({
			code: 2,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var data = [],
				jd = jStaticData.data;
			for (var i = 0, l = jd.length; i < l; i++) {
				data.push(jd[i]['vivacious']);
			}
			initEcharts({
				render: 'vivacious-device-chart',
				type: 'bar',
				xAxis: [{
					data: ['iphone', 'ipad', 'android']
				}],
				yAxis: [{
					axisLabel: {
						formatter: function(value) {
							return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
						}
					}
				}],
				series: [{
					data: data
				}]
			});
		});
		//常用设备
		getDataInterFace({
			code: 2,
			type: 2
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [];
			for (var i = 0, l = jd.length; i < l; i++) {
				data.push(jd[i]['logincount']);
			}
			initEcharts({
				render: 'common-device-login-month-chart',
				type: 'bar',
				xAxis: [{
					data: ['iphone', 'ipad', 'android']
				}],
				yAxis: [{
					axisLabel: {
						formatter: function(value) {
							return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
						}
					}
				}],
				series: [{
					data: data
				}]
			});
		});
		//平均设备数量
		getDataInterFace({
			code: 2,
			type: 3
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.device-average'),
				jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['device']);
			$('.panel-body-text sub', $Container).html('');
		});
		//登录时段
		getDataInterFace({
			code: 2,
			type: 4
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [];
			for (var i = 0, l = jd.length; i < l; i++) {
				data.push(jd[i]['logincount']);
			}

			initEcharts({
				render: 'login-timeslot-chart',
				type: 'line',
				xAxis: [{
					data: ['夜间', '晨会', '上午工作', '午休', '下午工作', '晚饭', '晚上']
				}],
				yAxis: [{
					axisLabel: {
						formatter: function(value) {
							return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
						}
					}
				}],
				series: [{
					data: data
				}]
			});
		});
	}
	/**
	 * 注册
	 */

function initCode3() {
		//手机注册数
		getDataInterFace({
			code: 3,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$ContainerP = $('.phone-registered'),
				$ContainerEmail = $('.email-registered'),
				$ContainerUserName = $('.registered-username'),
				jd = jStaticData.data;
			//手机用户注册
			$('.panel-body-text em', $ContainerP).html(jd['phone']);
			$('.panel-body-text sub', $ContainerP).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
			//手机用户注册
			$('.panel-body-text em', $ContainerEmail).html(jd['email']);
			$('.panel-body-text sub', $ContainerEmail).html(parseFloat(jd['email'] * 100 / jStaticData['total']).toFixed(2) + '%');
			//手机用户注册
			$('.panel-body-text em', $ContainerUserName).html(jd['newusers']);
			$('.panel-body-text sub', $ContainerUserName).html(parseFloat(jd['newusers'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
	}
	/**
	 * 客户
	 */

function initCode4() {
		//使用通讯录导入功能比例
		getDataInterFace({
			code: 4,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [{
					value: jd['mailimport'],
					name: '通讯录导入'
				}, {
					value: Math.abs(jd['newusers'] - jd['mailimport']),
					name: ''
				}];
			initEcharts({
				render: 'import-mail-scale-chart',
				type: 'pie',
				text: parseFloat(jd['mailimport'] * 100 / jd['newusers']).toFixed(2) + '%',
				series: [{
					radius: ['0%', '100%'],
					data: data
				}]
			});
		}); //手动添加直接完善信息比例
		getDataInterFace({
			code: 4,
			type: 2
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [{
					value: jd['manual'],
					name: '手动添加直接完善信息'
				}, {
					value: Math.abs(jd['newusers'] - jd['manual']),
					name: ''
				}];
			initEcharts({
				render: 'manual-scale-chart',
				type: 'pie',
				text: parseFloat(jd['manual'] * 100 / jd['newusers']).toFixed(2) + '%',
				series: [{
					radius: ['0%', '100%'],
					data: data
				}]
			});
		});
		//有沟通记录客户比例
		getDataInterFace({
			code: 4,
			type: 4
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [{
					value: jd['communicate'],
					name: '有沟通记录客户比例'
				}, {
					value: Math.abs(jd['newusers'] - jd['communicate']),
					name: ''
				}];
			initEcharts({
				render: 'has-communicate-record-scale-chart',
				type: 'pie',
				text: parseFloat(jd['communicate'] * 100 / jd['newusers']).toFixed(2) + '%',
				series: [{
					radius: ['0%', '100%'],
					data: data
				}]
			});
		});
		//修改客户信息次数
		getDataInterFace({
			code: 4,
			type: 3
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.modify-customerinfo-count'),
				jd = jStaticData.data;
			//修改客户信息次数
			$('.panel-body-text em', $Container).html(jd['modify']);
			$('.panel-body-text sub', $Container).html('');

			$('.panel-bottom', $Container).html('<em class="number-font">max:' + jd['maxmodify'] + '</em><em class="number-font">min:' + jd['minmodify'] + '</em>');
		});
		//沟通记录输入字数
		getDataInterFace({
			code: 4,
			type: 6
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.communicate-record-words'),
				jd = jStaticData.data;
			//修改客户信息次数
			$('.panel-body-text em', $Container).html(jd['words']);
			$('.panel-body-text sub', $Container).html('');

			$('.panel-bottom', $Container).html('<em class="number-font">max:' + jd['maxwords'] + '</em><em class="number-font">min:' + jd['minwords'] + '</em>');
		});
		//录入沟通记录时间段
		getDataInterFace({
			code: 4,
			type: 5
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				data = [];
			for (var i = 0, l = jd.length; i < l; i++) {
				data.push(jd[i]['communicate']);
			}

			initEcharts({
				render: 'input-communicate-record-chart',
				type: 'line',
				xAxis: [{
					data: ['夜间', '晨会', '上午工作', '午休', '下午工作', '晚饭', '晚上']
				}],
				yAxis: [{
					axisLabel: {
						formatter: function(value) {
							return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
						}
					}
				}],
				series: [{
					data: data
				}]
			});
		});
	}
	/**
	 * 提醒
	 */

function initCode5() {
		/*//使用自定义提醒的用户数（A）
		getDataInterFace({
			code: 5,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.use-custom-reminder')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//日均建自定义提醒数
		getDataInterFace({
			code: 5,
			type: 2
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.build-custom-reminder-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当周均建自定义提醒数
		getDataInterFace({
			code: 5,
			type: 3
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.build-custom-reminder-week')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当月均建自定义提醒数
		getDataInterFace({
			code: 5,
			type: 4
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.build-custom-reminder-month')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//日均修改自定义提醒数
		getDataInterFace({
			code: 5,
			type: 5
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.modify-custom-reminder-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//当周均修改自定义提醒数
		getDataInterFace({
			code: 5,
			type: 6
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.modify-custom-reminder-week')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//当月均修改自定义提醒数
		getDataInterFace({
			code: 5,
			type: 7
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.modify-custom-reminder-month')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['customreminder']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});*/
		initChartToDOM(5);
	}
	/**
	 * 统计
	 */

function initCode6() {
		/*//使用自定义提醒的用户数（A）
		getDataInterFace({
			code: 6,
			type: 1
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.view-static-users-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//日均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 2
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-static-users-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当周均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 3
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-static-users-week')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当月均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 4
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-static-users-month')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//使用自定义提醒的用户数（A）
		getDataInterFace({
			code: 6,
			type: 5
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.view-teamstatic-users-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewteamstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});
		//日均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 6
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-teamstatic-users-day')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewteamstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当周均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 7
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-teamstatic-users-week')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewteamstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		}); //当月均建自定义提醒数
		getDataInterFace({
			code: 6,
			type: 8
		}, function(jStaticData) {
			if (!jStaticData) return false;
			jStaticData = $.parseJSON(jStaticData);
			var jd = jStaticData.data,
				$Container = $('.avg-view-teamstatic-users-month')
			jd = jStaticData.data;
			$('.panel-body-text em', $Container).html(jd['viewteamstatic']);
			//$('.panel-body-text sub', $Container).html(parseFloat(jd['phone'] * 100 / jStaticData['total']).toFixed(2) + '%');
		});*/
		initChartToDOM(6);
	}
	/**
	 * 分享
	 */

function initCode7() {
		initChartToDOM(7);
	}
	/**
	 * 流失统计
	 */

function initCode8() {
		initChartToDOM(8);
	}
	/**
	 * 统计数据到对应的DOM节点上
	 * @param {Object} code 统计类型编码
	 */

function initChartToDOM(code) {
		$('div[code="' + code + '"] div[data-type][data-chart-type]').each(function() {
			var tar = $(this),
				dataType = tar.attr('data-type'),
				chartType = tar.attr('data-chart-type'),
				chartColumn = tar.attr('data-chart-column'),
				chartXaxisColumn = tar.attr('data-chart-xaxis-column'),
				chartRender = tar.attr('data-chart-render');
			getDataInterFace({
				code: code,
				type: parseInt(dataType)
			}, function(jStaticData) {
				if (!jStaticData) return false;
				jStaticData = $.parseJSON(jStaticData);
				var jd = jStaticData.data,
					data = [],
					xaxis = [];
				//一般数字显示
				if (chartType == 'text') {
					$('.panel-body-text em', tar).html(jd[chartColumn]);
				}
				//柱状图
				if (chartType == 'bar') {
					for (var i = 0, l = jd.length; i < l; i++) {
						data.push(jd[i][chartColumn]);
						xaxis.push(jd[i][chartXaxisColumn])
					}
					setBarEcharts({
						render: chartRender,
						type: chartType,
						xaxis: xaxis,
						data: data
					});
				}
			});
		});
	}
	/**
	 * 柱状统计图
	 * @param {Object} c 配置参数{render:'',type:'',xaxis:[],data:[]}
	 */

function setBarEcharts(c) {
	initEcharts({
		render: c.render,
		type: c.type,
		xAxis: [{
			data: c.xaxis
		}],
		yAxis: [{
			axisLabel: {
				formatter: function(value) {
					return value < 1000 ? value : (parseFloat(value / 1000).toFixed(1) + 'K');
				}
			}
		}],
		series: [{
			data: c.data
		}]
	});
}